package com.lungle.warmdiary.app.view.active.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.base.BaseViewPagerAdapter;
import com.lungle.warmdiary.app.view.active.ActiveDetailTab;
import com.lungle.warmdiary.app.view.active.fragment.status.ActiveFragment;

public final class ActiveDetailTabPagerAdapter extends BaseViewPagerAdapter {

    public ActiveDetailTabPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public final int getCount() {
        return ActiveDetailTab.values().length;
    }

    public final CharSequence getPageTitle(int i) {
        ActiveDetailTab tab = ActiveDetailTab.getTabByIdx(i);
        int resId = 0;
        CharSequence title = "";
        if (tab != null)
            resId = tab.getTitle();
        if (resId != 0)
            title = AppContext.string(resId);
        return title;
    }

    @Override
    protected Fragment createItem(int position) {
        final int pattern = position % 5;
        ActiveDetailTab[] values = ActiveDetailTab.values();
        Fragment f = null;
        try {
            f = (Fragment) values[pattern].getClz().newInstance();

            Bundle args = new Bundle();
            args.putInt(ActiveFragment.BUNDLE_KEY_CATALOG, values[pattern].getCatalog());
            f.setArguments(args);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return f;
    }
}